package com.dstz.bpm.api.model.nodedef.impl;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.bpm.api.constant.NodeType;
import com.dstz.bpm.api.engine.action.button.ButtonFactory;
import com.dstz.bpm.api.engine.plugin.context.BpmPluginContext;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.def.BpmProcessDef;
import com.dstz.bpm.api.model.def.NodeInit;
import com.dstz.bpm.api.model.def.NodeProperties;
import com.dstz.bpm.api.model.form.BpmForm;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.model.nodedef.Button;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BaseBpmNodeDef implements BpmNodeDef {
   private static final long serialVersionUID = -2044846605763777657L;
   private String nodeId;
   private String name;
   private NodeType type;
   private BpmNodeDef parentBpmNodeDef;
   private List<BpmNodeDef> incomeNodes = new ArrayList();
   private List<BpmNodeDef> outcomeNodes = new ArrayList();
   private List<BpmPluginContext> bpmPluginContexts = new ArrayList();
   private Map<String, String> attrMap = new HashMap();
   private BpmProcessDef bpmProcessDef;
   private BpmForm mobileForm;
   private BpmForm form;
   private List<NodeInit> nodeInits = new ArrayList();
   private NodeProperties nodeProperties = new NodeProperties();
   private List<Button> buttons = null;
   private List<Button> buttonList = null;

   public String getNodeId() {
      return this.nodeId;
   }

   public void setNodeId(String nodeId) {
      this.nodeId = nodeId;
   }

   public String getName() {
      return this.name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public NodeType getType() {
      return this.type;
   }

   public void setType(NodeType type) {
      this.type = type;
   }

   public List<BpmNodeDef> getIncomeNodes() {
      return this.incomeNodes;
   }

   public void setIncomeNodes(List<BpmNodeDef> incomeNodes) {
      this.incomeNodes = incomeNodes;
   }

   public List<BpmNodeDef> getOutcomeNodes() {
      return this.outcomeNodes;
   }

   public void setOutcomeNodes(List<BpmNodeDef> outcomeNodes) {
      this.outcomeNodes = outcomeNodes;
   }

   public List<BpmPluginContext> getBpmPluginContexts() {
      return this.bpmPluginContexts;
   }

   public void setBpmPluginContexts(List<BpmPluginContext> bpmPluginContexts) {
      Collections.sort(bpmPluginContexts);
      this.bpmPluginContexts = bpmPluginContexts;
   }

   public void setAttribute(String name, String value) {
      name = name.toLowerCase().trim();
      this.attrMap.put(name.toLowerCase(), value);
   }

   public String getAttribute(String name) {
      name = name.toLowerCase().trim();
      return (String)this.attrMap.get(name);
   }

   public void addIncomeNode(BpmNodeDef bpmNodeDef) {
      this.incomeNodes.add(bpmNodeDef);
   }

   public void addOutcomeNode(BpmNodeDef bpmNodeDef) {
      this.outcomeNodes.add(bpmNodeDef);
   }

   public BpmNodeDef getParentBpmNodeDef() {
      return this.parentBpmNodeDef;
   }

   public void setParentBpmNodeDef(BpmNodeDef parentBpmNodeDef) {
      this.parentBpmNodeDef = parentBpmNodeDef;
   }

   public String getRealPath() {
      String id = this.getNodeId();

      for(BpmNodeDef parent = this.getParentBpmNodeDef(); parent != null; parent = parent.getParentBpmNodeDef()) {
         id = parent.getNodeId() + "/" + id;
      }

      return id;
   }

   public BpmProcessDef getBpmProcessDef() {
      return this.bpmProcessDef;
   }

   public void setBpmProcessDef(BpmProcessDef bpmProcessDef) {
      this.bpmProcessDef = bpmProcessDef;
   }

   public BpmProcessDef getRootProcessDef() {
      BpmProcessDef processDef = this.bpmProcessDef;

      for(BpmProcessDef parent = processDef.getParentProcessDef(); parent != null; parent = parent.getParentProcessDef()) {
         processDef = parent;
      }

      return processDef;
   }

   public List<BpmNodeDef> getOutcomeTaskNodes() {
      return this.getNodeDefList(this.outcomeNodes);
   }

   private List<BpmNodeDef> getNodeDefList(List<BpmNodeDef> bpmNodeDefs) {
      List<BpmNodeDef> bpmNodeList = new ArrayList();
      Iterator var3 = bpmNodeDefs.iterator();

      while(true) {
         while(true) {
            while(var3.hasNext()) {
               BpmNodeDef def = (BpmNodeDef)var3.next();
               if (!NodeType.USERTASK.equals(def.getType()) && !NodeType.SIGNTASK.equals(def.getType())) {
                  SubProcessNodeDef subProcessNodeDef;
                  if (NodeType.SUBPROCESS.equals(def.getType())) {
                     subProcessNodeDef = (SubProcessNodeDef)def;
                     BpmNodeDef startNodeDef = subProcessNodeDef.getChildBpmProcessDef().getStartEvent();
                     bpmNodeList.addAll(this.getNodeDefList(startNodeDef.getOutcomeNodes()));
                  } else if (NodeType.END.equals(def.getType()) && def.getParentBpmNodeDef() != null && NodeType.SUBPROCESS.equals(def.getParentBpmNodeDef().getType())) {
                     subProcessNodeDef = (SubProcessNodeDef)def.getParentBpmNodeDef();
                     bpmNodeList.addAll(this.getNodeDefList(subProcessNodeDef.getOutcomeNodes()));
                  } else {
                     bpmNodeList.addAll(this.getNodeDefList(def.getOutcomeNodes()));
                  }
               } else {
                  bpmNodeList.add(def);
               }
            }

            return bpmNodeList;
         }
      }
   }

   public List<BpmNodeDef> getInnerOutcomeTaskNodes(boolean includeSignTask) {
      List<BpmNodeDef> bpmNodeList = this.getInnerOutcomeTaskNodes(this.outcomeNodes, includeSignTask);
      return bpmNodeList;
   }

   private List<BpmNodeDef> getInnerOutcomeTaskNodes(List<BpmNodeDef> bpmNodeDefs, boolean includeSignTask) {
      List<BpmNodeDef> bpmNodeList = new ArrayList();
      Iterator var4 = bpmNodeDefs.iterator();

      while(true) {
         while(var4.hasNext()) {
            BpmNodeDef def = (BpmNodeDef)var4.next();
            if (!NodeType.USERTASK.equals(def.getType()) && (!includeSignTask || !NodeType.SIGNTASK.equals(def.getType()))) {
               if (!NodeType.SUBPROCESS.equals(def.getType()) && !NodeType.CALLACTIVITY.equals(def.getType()) && !NodeType.END.equals(def.getType())) {
                  bpmNodeList.addAll(this.getInnerOutcomeTaskNodes(def.getOutcomeNodes(), includeSignTask));
               }
            } else {
               bpmNodeList.add(def);
            }
         }

         return bpmNodeList;
      }
   }

   public <T> T getPluginContext(Class<T> cls) {
      BpmPluginContext ctx = null;
      List<BpmPluginContext> list = this.bpmPluginContexts;
      Iterator var4 = list.iterator();

      while(var4.hasNext()) {
         BpmPluginContext context = (BpmPluginContext)var4.next();
         if (context.getClass().isAssignableFrom(cls)) {
            ctx = context;
            break;
         }
      }

      return (T) ctx;
   }

   public NodeProperties getNodeProperties() {
      return this.nodeProperties;
   }

   public void setNodeProperties(NodeProperties nodeProperties) {
      this.nodeProperties = nodeProperties;
   }

   public void setButtons(List<Button> buttons) {
      this.buttons = buttons;
   }

   public List<Button> getButtons() {
      if (this.buttons != null) {
         return this.buttons;
      } else if (this.buttonList != null) {
         return this.buttonList;
      } else {
         try {
            this.buttonList = ButtonFactory.generateButtons(this, true);
         } catch (Exception var2) {
            throw new BusinessException(BpmStatusCode.TASK_ACTION_BTN_ERROR, var2);
         }

         return this.buttonList;
      }
   }

   public boolean isDefaultBtn() {
      return this.buttons == null;
   }

   public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append(this.getName()).append(":").append(this.getNodeId()).append(super.toString());
      return sb.toString();
   }

   public BpmForm getMobileForm() {
      return this.mobileForm;
   }

   public void setMobileForm(BpmForm mobileForm) {
      this.mobileForm = mobileForm;
   }

   public BpmForm getForm() {
      return this.form;
   }

   public void setForm(BpmForm form) {
      this.form = form;
   }
}
